import Vue from 'vue'
import $ from 'jquery'

import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'

Vue.use(ElementUI);
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(MintUI)
Vue.http.options.emulateJSON = true

import login from './App.vue'
import router from './router.js'
import './lib/mui/css/mui.min.css'


import '../src/lib/iconfont.js'
import Vuex from 'vuex'
Vue.use(Vuex)


var car = JSON.parse(localStorage.getItem('car') || '[]')
var username = JSON.parse(localStorage.getItem('username'))

var store = new Vuex.Store({
    state:{
        car:car,  //将购物车中的商品数据，用一个数组存储起来
        username:username

    },
    mutations:{
        recar(state,car){
            state.car=car
         localStorage.setItem ('car',JSON.stringify(state.car))
        },

       addToCar(state,bbinfo){

            var flag = false

            state.car.some(item=>{
                if(item.id == bbinfo.id){
                    item.count += parseInt(bbinfo.count)
                    flag = true
                    return true
                }
            })

            if(!flag){
                state.car.push(bbinfo)
            }
            //当更新car之后，把car数组，存储到本地的localStorage中
            localStorage.setItem ('car',JSON.stringify(state.car))

       },
       
       //保存用户登录名
       login(state,username){
            state.username=username
            localStorage.setItem ('username',JSON.stringify(state.username))
       },
       updateGoodsInfo(state,goodsinfo){
        state.car.some(item => {
            if(item.id == goodsinfo.id){
                item.count = parseInt(goodsinfo.count)
                return true
            }
        })
        localStorage.setItem('car',JSON.stringify(state.car))
    },
    remove(state,id){
        //根据id删除
        state.car.some((item,i)=>{
            if(item.id == id){
                state.car.splice(i,1)
                return true;
            }
        })
        localStorage.setItem ('car',JSON.stringify(state.car))
    },
    updateSelected(state,info){
        state.car.some(item=>{
            if(item.id == info.id){
                item.selected = info.selected
            }
        })
        localStorage.setItem ('car',JSON.stringify(state.car))
    }


    },
    getters:{
        getusername(state){
            var u =''
            u=state.username
            return u
        },
        getAllCount(state){
            var c = 0
            state.car.forEach(item=>{
                c += item.count
            })
            return c
        },
        getGoodsCount(state){
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.count
            })
            return o
        },
        getGoodsSelected(state){
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.selected
            })
            return o
        },
        getGoodsAndmount(state){
            var o = {
                count:0,
                amount:0
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.count += item.count
                    o.amount += item.price * item.count
                }
            })
            return o
        }
    }
})


















var vm = new Vue({
    el:'#app',
    data:{
        
    },
    
    // render:function(createElements){
    //     return createElements(login)
    // }
    render:c => c(login),
    router,
    //挂载状态管理对象
    store
})
